#include <stdio.h>
#include "linkedlist_basic.h"

#define NUMELEMENT 7

// Insert a node at the head of the linklist
void insertInFront(NODE** head, int data) {
    NODE* newNode = new NODE;
    
    newNode->data = data;
    newNode->next = *head;
    
    *head = newNode;
}

// Count the number of nodes in the linklist
int length(NODE* head) {
    int count = 0;
    NODE* currentNode = head;
    while (currentNode != NULL) {
        count++;
        currentNode = currentNode->next;
    }
    return count;
}

// Traverse the linklist
void traverseList(NODE* head) {
    NODE* currentNode = head;
    while (currentNode != NULL) {
        printf("Data in the current node = %d\n", currentNode->data);
        currentNode = currentNode->next;
    }
}

// Remove a node from the head of the linklist
// return false if the node is e
bool removeAtFront(NODE** head, int* value) {
    NODE* currentNode = *head;
    if (head == NULL)
        return false;
    
    *value = currentNode->data;
    *head = currentNode->next;
    delete currentNode;
    return true;
}


void test_linklist_basic() {
	NODE* head = NULL;
	int data[NUMELEMENT] = {3, 4, 1, 8, 6, 10, 0};

	printf("Start with the empty list\n");    
	for (int i = 0; i < NUMELEMENT; i++ ) {
		printf("Insert %d at the front of the list\n", data[i]);
		insertInFront(&head, data[i]);
	}

	printf("\nNumber of nodes in this list = %d\n\n", length(head));

	printf("Traversing the list\n");
	traverseList(head);
	printf("\n");

	int value;
	for (int i = 0; i < 2; i++ ) {
		removeAtFront(&head, &value);
		printf("Remove one node from the head: %d\n", value);		
	}
	printf("\n");

	printf("\nNumber of nodes in this list = %d\n\n", length(head));
	
	printf("Traversing the list\n");
	traverseList(head);
	printf("\n");
}
